package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.EquipmentMapper;
import com.java2207.group1.pojo.Equipment;
import com.java2207.group1.service.EquipmentService;
import com.java2207.group1.vo.equipment.BarVo;
import com.java2207.group1.vo.equipment.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("equipmentService")
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public LayuiPageVo<Equipment> equipmentPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        // mybatis 的分页查询
        PageHelper.startPage(page,limit);

        List<Equipment> equipments = equipmentMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<Equipment> equipmentPageInfo = new PageInfo<>(equipments);

        // 把 mybatis 的分页数据封装到 layui 的分页数据结构中
        LayuiPageVo<Equipment> equipmentLayuiPageVo = new LayuiPageVo<>();

        equipmentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        equipmentLayuiPageVo.setMsg("分页列表数据");  // 提示信息
        equipmentLayuiPageVo.setCount(equipmentPageInfo.getTotal());  // 总记录数
        equipmentLayuiPageVo.setData(equipmentPageInfo.getList());  // 分页的列表数据
        return equipmentLayuiPageVo;
    }

    @Override
    public int addEquipment(Equipment equipment) {

        return equipmentMapper.addEquipment(equipment);
    }

    @Override
    public int deleteEquipment(Integer id) {
        return equipmentMapper.deleteEquipment(id);
    }

    @Override
    public int updateEquipment(Equipment equipment) {
        return equipmentMapper.updateEquipment(equipment);
    }

    @Override
    public int batchEquipment(String ids) {
        String[] arr = ids.split(",");
        return equipmentMapper.batchEquipment(arr);
    }

    @Override
    public List<BarVo> setBarData() {
        return equipmentMapper.setBarData();
    }

    public List<PieVo> setPieData(){
        return equipmentMapper.setPieData();
    }


}
